const Router = require('koa-router');
const tool = require("../../tools/tool");
const router = new Router();
const {db, Op, QueryTypes} = require("../../models/db");
// 余额字段明细点击显示关连票据
router.post('/list/getRelBillByBillId', async ctx => {
    try {
        let user = ctx.user
        let body = ctx.request.body
        let tenantId = user.tenantId;
        let billNo = body.billNo;
        let transType = body.transType;
        let id = 0;
        let ret = {
            list2: [],
            list3: []
        }

        if (transType) {
            if ([153001, 153101].indexOf(transType) !== -1) {
                let list2 = await db.query(`select * from v_invoice_receipt_detail where billNo='${billNo}' and tenantId=${tenantId}`, {type: QueryTypes.SELECT})
                id = list2[0].id;
                ret.list2 = list2;
                return ctx.suc('', ret);
            }
            else if ([150501, 150502, 150601, 150602, 150603].indexOf(transType) !== -1) {
                let list2 = await db.query(`select * from v_invoice_detail where billNo='${billNo}' and tenantId=${tenantId}`, {type: QueryTypes.SELECT})
                id = list2[0].id;
                ret.list2 = list2;
                return ctx.suc('', ret);
            }
            else if (transType === 153403) {
                let list2 = await db.query(`select * from v_invoice_detail_ys where billNo='${billNo}' and tenantId=${tenantId}`, {type: QueryTypes.SELECT})
                id = list2[0].id;
                ret.list2 = list2;
                return ctx.suc('', ret);
            }
            else if (transType === 160001) {
                let list2 = await db.query(`select * from v_invoice_trans where billNo='${billNo}' and tenantId=${tenantId}`, {type: QueryTypes.SELECT})
                id = list2[0].id;
                ret.list2 = list2;
                return ctx.suc('', ret);
            }
            else {
                let list2 = await db.query(`select * from v_invoice_other_detail where billNo='${billNo}' and tenantId=${tenantId}`, {type: QueryTypes.SELECT})
                id = list2[0].id;
                ret.list2 = list2;
                return ctx.suc('', ret);
            }
            if (id !== 0) {
                let list3 = await db.query(`select * from v_card_detail where byIid=${id} and tenantId=${tenantId} order by price1 desc `, {type: QueryTypes.SELECT})
                id = list3[0].id;
                ret.list3 = list3;
                return ctx.suc('', ret);
            }
        }
        if (billNo.indexOf('PZ') === 0) {
            let list3 = await db.query(`select * from v_card_detail where billNo='${billNo}' and tenantId=${tenantId} order by price1 desc `, {type: QueryTypes.SELECT})
            ret.list3 = list3;
            return ctx.suc('', ret);
        }
    } catch (e) {
        return ctx.err(e);
    }
})
module.exports = router;